<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/30
 * Time: 12:48
 */
namespace App\Forms;

use App\Exceptions\RequestException;
use App\Models\Good;
use App\Models\GoodsSku;
use App\Models\GoodsSkuAttribute;
use App\Models\GoodsSkuDetail;
use App\Models\User;

class GoodsAttributeForms extends BaseForm
{


    /**
     * 获取分类sku列表数据
     * @param $validated
     * @return mixed
     */
    public function getSkuList($validated)
    {
        return GoodsSku::where([])
            ->page($validated['page'], $validated['limit'])
            ->orderBy('id','desc')
            ->get();
    }


    /**
     * 获取sku详情
     * @param $validated
     * @return GoodsSku|\Illuminate\Database\Eloquent\Model
     */
    public function getSkuDetail($validated)
    {
        return GoodsSku::firstOrNew(['id' => $validated['id'] ?? 0]);
    }


    /**
     * 编辑sku
     * @param $validated
     * @return GoodsSku|\Illuminate\Database\Eloquent\Model
     */
    public function editSku($validated)
    {

        if(isset($validated['sku_value']) && is_array($validated['sku_value'])){
            $sku_value = $validated['sku_value'];
        }else{
            $sku_value = [];
        }

        $validated['sku_value'] = json_encode($sku_value, JSON_UNESCAPED_UNICODE);

        return GoodsSku::updateOrCreate(['id' => $validated['id'] ?? 0], $validated);
    }


    /**
     * 删除分类
     * @param $validated
     * @return bool|mixed|null
     */
    public function delete($validated)
    {
        return GoodsSku::whereKey($validated['id'])->delete();
    }


    /**
     * 前台显示价格
     * @param array $list
     * @return array
     */
    public function setSkuAmount(Array $list)
    {
        foreach ($list as &$item){
            $sku = $this->setSkuAmountItem($item['id']);
            $item['price'] = $sku['price'];
            $item['market_price'] = $sku['market_price'];
        }

        return $list;
    }


    /**
     * @param $goods_id
     * @return array
     */
    public function setSkuAmountItem($goods_id)
    {
        $sku = GoodsSkuAttribute::where('goods_id', $goods_id)->firstOrNew();

        return [
            'price'        => $sku->price ?: 0,
            'market_price' => $this->dealPrice((new UserForms())->getUserDetail(UID), $sku)
        ];
    }

    /**
     * 单规格
     * @param $goods_id
     * @return GoodsSkuAttribute|\Illuminate\Database\Eloquent\Model
     */
    public function getGoodsAttributeListSingle($goods_id)
    {
        return GoodsSkuAttribute::firstOrNew(['goods_id' => $goods_id]);
    }

    /**
     * 单规格详情
     * @param $aid
     * @return GoodsSkuAttribute|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getGoodsAttributeDetail($aid)
    {
        return GoodsSkuAttribute::whereKey($aid)->first();
    }


    /**
     * 获取商品信息
     * @param GoodsSkuAttribute $attribute
     * @param User $user
     * @return array
     */
    public function getGoodsPreview(GoodsSkuAttribute $attribute, User $user)
    {

        $goods = Good::whereKey($attribute->goods_id)->first(['id', 'images', 'goods_name', 'goods_description'])->toArray();

        $goods['images']       = $attribute->sku_images ?: $goods['images'];
        $goods['market_price'] = $attribute->market_price; //市场价  正常价格
        $goods['pay_price']    = $this->dealPrice($user, $attribute); //折扣价
        $goods['price']        = $attribute->price; //原价（应该用不到）
        $goods['weight']       = $attribute->weight;

        $sku = [];
        if($attribute->attribute_name){
            foreach (json_decode($attribute->attribute_name, true) as $k => $v){
                $sku[] = [GoodsSkuDetail::whereKey(array_key_first($v))->value('value') .':'.$v[array_key_first($v)]];
            }
        }

        $goods['sku'] = $sku;

        return $goods;
    }


    /**
     * 处理价格
     * @param User $user
     * @param GoodsSkuAttribute $attribute
     * @return float|null
     */
    public function dealPrice(User $user, GoodsSkuAttribute $attribute)
    {
        switch ($user->level){
            case User::L_DEFAULT:
                return $attribute->market_price;
            case User::L_DISTRIBUTOR:
                return $attribute->distributor_price;
            case User::L_PARTNER:
                return $attribute->partner_price;
            default:
                return $attribute->market_price;
        }
    }

    /**
     * 规格列表
     * @param $goods_id
     * @return \Illuminate\Support\Collection
     */
    public function getGoodsAttributeList($goods_id)
    {
        return GoodsSkuAttribute::where('goods_id', $goods_id)->get();
    }
}